package me.itsoo.artemis.framework.repository.mapper.condition;

import lombok.Getter;

import java.util.Objects;
import java.util.StringJoiner;

/**
 * PageCondition
 *
 * @author zxy
 * @version 0.0.1
 * @since 2023/5/17
 */
@Getter
public class PageCondition extends AbstractCondition {

    private int pageNum;

    private int pageSize;

    private QueryCondition queryCond;

    public PageCondition() {
        this.pageNum = 1;
        this.pageSize = 10;
    }

    /**
     * 创建空 PageCondition
     *
     * @return PageCondition
     */
    public static PageCondition create() {
        return new PageCondition();
    }

    /**
     * 设置 pageNum
     *
     * @param pageNum int
     * @return PageCondition
     */
    public PageCondition pageNum(int pageNum) {
        this.pageNum = pageNum;
        return this;
    }

    /**
     * 设置 pageSize
     *
     * @param pageSize int
     * @return PageCondition
     */
    public PageCondition pageSize(int pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    /**
     * 原始查询条件
     *
     * @param queryCond QueryCondition
     * @return PageCondition
     */
    public PageCondition queryCond(QueryCondition queryCond) {
        this.queryCond = queryCond;
        return this;
    }

    @Override
    public String getConditionCommand() {
        final StringJoiner result = new StringJoiner(" ");
        if (Objects.nonNull(queryCond)) {
            result.add(queryCond.getConditionCommand());
        }

        result.add("limit");
        result.add(String.valueOf((pageNum - 1) * pageSize));
        result.add(",");
        result.add(String.valueOf(pageSize));
        return result.toString();
    }

    @Override
    public Object[] getConditionArgs() {
        final Object[] emptyArgs = new Object[0];
        return Objects.nonNull(queryCond) ? queryCond.getConditionArgs() : emptyArgs;
    }
}
